package LCR;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class findAnagrams {
    public static void main(String[] args) {
        String s = "cbaebabacd";
        String s2 = "abc";
        List<Integer> ret =   findAnagrams(s,s2);
        for(int x : ret) {
            System.out.print(x+" ");
        }


    }
    public static List<Integer> findAnagrams(String s, String p) {
        int n = p.length();

        if(n > s.length()) {
            return new ArrayList<>();
        }
        List<Integer> ret = new ArrayList<>();
        int[] cnt1 = new int[26];
        int[] cnt2 = new int[26];
        int i = 0;
        while(i < n) {
            cnt1[s.charAt(i)-'a']++;
            cnt2[p.charAt(i)-'a']++;
            i++;
        }
        if(Arrays.equals(cnt1,cnt2)) {
            ret.add(0);
        }
        for(i =n;i < s.length();i++) {
            cnt1[s.charAt(i-n)-'a']--;
            cnt1[s.charAt(i)-'a']++;
            if(Arrays.equals(cnt1,cnt2)) {
                ret.add(i-n+1);
            }
        }
        return ret;

    }

}
